package net.crazyadam.audio;

/**
 * Copyright by <a href="http://crazyadam.net"><em><i>Joseph J.C. Tang</i></em></a> <br/>
 * Email: <a href="mailto:jinntrance@gmail.com">jinntrance@gmail.com</a>
 *
 * @author joseph
 * @version 1.0
 * @since 9/20/12 8:30 PM
 */
public class MathFunction {
    public static float mean(float[] number){
       float sum=0;
       int count=0;
        for(float n:number){
            if(n!=0){
                sum+=n;
                count++;
            }
        }
        return sum/count;
    }
    public static int mean(int[] number){
        int sum=0;
        int count=0;
        for(int n:number){
            if(n!=0){
                sum+=n;
                count++;
            }
        }
        return sum/count;
    }
    public static  float expectOfSquare(float[] number){
        float sum=0;
        for(float n:number){
            sum+=n*n;
        }
        return sum/number.length;
    }
    public static  double deviation(float[] numbers){
        return expectOfSquare(numbers)-Math.pow(mean(numbers),2);
    }
    public static float max(float[] numbers){
      float max=0;
        for(float n:numbers){
            if(n>max) max=n;
        }
        return  max;
    }

    /**
     *
     * @param numbers
     * @param startIndex
     * @param endIndex
     * @return 0,if arguments error
     */
    public static float  max(float[]numbers,int startIndex,int endIndex){
        float max=0;
        if(startIndex<endIndex&&endIndex<=numbers.length){
           for(int i=startIndex;i<endIndex;i++){
              if(max<numbers[i])
                  max=numbers[i];
           }
        }
        return max;
    }
    public static int maxIndex(float[]numbers,int startIndex,int endIndex){
        float max=0;
        int index=0;//index of the max value
        if(startIndex<endIndex&&endIndex<=numbers.length){
            for(int i=startIndex;i<endIndex;i++){
                if(max<numbers[i]) {
                    index=i;
                    max=numbers[i];
                }
            }
        }
       return index;
    }
    public static int positiveIndex(float[]numbers,int startIndex,int endIndex){
        int index=0;//index of the max value
        if(startIndex<endIndex&&endIndex<=numbers.length){
            for(int i=startIndex;i<endIndex;i++){
                if(0<numbers[i]) {
                    index=i;
                }
            }
        }
        return index;
    }
    public static int positiveIndex(float[]numbers){
       return positiveIndex(numbers,0,numbers.length);
    }
    public  static  float[] sub(float[] n1,float [] n2){
        float[] r=new float[n1.length];
        for(int i=0;i<n1.length;i++){
            r[i]=n1[i]-n2[i];
        }
        return r;
    }

    public static int maxIndex(float[] numbers) {
        return maxIndex(numbers,0,numbers.length);
    }
    public static float mean(float[]data,float threshold){
        float sum=0f;
        int count=0;
        for (float aData : data) {
            if (aData >= threshold && aData != 0) {
                sum += aData;
                count++;
            }
        }
        return sum/count;
    }
}
